package cn.edu.dlut.ssdut.foodCredit2012.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.edu.dlut.ssdut.foodCredit2012.model.PotentialUser;
import cn.edu.dlut.ssdut.foodCredit2012.model.User;
import cn.edu.dlut.ssdut.foodCredit2012.util.HibernateUtil;

public class UserService {
	private BasicService basicS = new BasicService();
	private CustomsService customsS = new CustomsService();
	private ExportService exportS = new ExportService();
	private FinanceService financeS = new FinanceService();
	private PermissionService permissionS = new PermissionService();
	private QualityService qualityS = new QualityService();
	private TaxService taxS = new TaxService();
	private PresentationService presentationS = new PresentationService();
	
	public void add(User u)throws Exception{
		if(!isUser(u)) {
			Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.save(u);
			session.getTransaction().commit();
		}
	}
	
	public void addInfo(User u) throws Exception{
		if(u.getInfo())
			return ;
			
		basicS.add(u.getBasic());
		customsS.add(u.getCustoms());
		exportS.add(u.getExport());
		financeS.add(u.getFinance());
		permissionS.add(u.getPermission());
		presentationS.add(u.getPresentation());
		qualityS.add(u.getQuality());
		taxS.add(u.getTax());
		
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		u = (User)session.get(User.class, u.getLicenceID());
		u.setInfo(true);
		
		session.flush();
		session.getTransaction().commit();
		
	}
	
	public User load(User u) throws Exception {
		if(isUser(u)){
			u = loadByLicenceID(u.getLicenceID());
			return u;
		}
		return u;
	}
	
	public User loadByLicenceID(String licenceID) throws Exception{
		User user = new User();
		
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		user=(User)session.get(User.class, licenceID);
		
			
		session.getTransaction().commit();
		
		return user;
	}
	
	public User loadUserInfo(User u) throws Exception{
		if(isUser(u)){
			
			if(u.getInfo()){
				
				u.setBasic(basicS.loadByLicenceID(u.getLicenceID()));
				u.setCustoms(customsS.loadByLicenceID(u.getLicenceID()));
				u.setExport(exportS.loadByLicenceID(u.getLicenceID()));
				u.setFinance(financeS.loadByLicenceID(u.getLicenceID()));
				u.setPermission(permissionS.loadByLicenceID(u.getLicenceID()));
				u.setPresentation(presentationS.loadByLicenceID(u.getLicenceID()));
				u.setQuality(qualityS.loadByLicenceID(u.getLicenceID()));
				u.setTax(taxS.loadByLicenceID(u.getLicenceID()));
				return u;
			}
			return u;
		}
		return u;
	}
	
	public void update (User u) throws Exception{
		if(isUser(u)){
			Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.update(u);
			session.getTransaction().commit();
		}
	}
	
	public void delete(User u) throws Exception{
		boolean exist = isUser(u);
		if(exist) {
			if(u.getInfo())
				deleteInfo(u);
			Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.delete(u);
			session.getTransaction().commit();
		}
	}
	
	public void deleteInfo(User u) throws Exception {
		boolean isUser = isUser(u);
		if(isUser) {
			if(u.getInfo()){
				basicS.delete(u.getBasic());
				customsS.delete(u.getCustoms());
				exportS.delete(u.getExport());
				financeS.delete(u.getFinance());
				permissionS.delete(u.getPermission());
				presentationS.delete(u.getPresentation());
				qualityS.delete(u.getQuality());
				taxS.delete(u.getTax());
				
				u.setInfo(false);
				update(u);
			}
		}
		
	}
	
	
	public List<User> userList() {
		List<User> userList = null;
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query query = session.createQuery("from User");
		/*query.setFirstResult(1);
		query.setMaxResults(10);*/
		userList = (List<User>)query.list();
		
		session.getTransaction().commit();
		
		return userList;
	}
	
	public List<User> userInfoList(){
		List<User> userList = null;
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query query = session.createQuery("select u from User u where u.info=:info")
			.setBoolean("info", true);
		/*query.setFirstResult(1);
		query.setMaxResults(10);*/
		userList = (List<User>)query.list();
		
		session.getTransaction().commit();
		
		return userList;
	}
	
	public List<User> userWithoutInfoList(){
		List<User> userList = null;
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query query = session.createQuery("select u from User u where u.info=:info")
			.setBoolean("info", false);
		/*query.setFirstResult(1);
		query.setMaxResults(10);*/
		userList = (List<User>)query.list();
		
		session.getTransaction().commit();
		
		return userList;
	}
	
	public boolean isUser(User u) throws Exception {
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		long count = (Long)session.createQuery("select count(*) from User u where u.licenceID = :licenceID")
			.setString("licenceID", u.getLicenceID())
			.uniqueResult();
		
		session.getTransaction().commit();
		
		if(count > 0) return true;
		return false;
		
	}
	
	public boolean login(User u) throws Exception {
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		long count = (Long)session.createQuery("select count(*) from User u where u.name = :name and u.licenceID = :licenceID and u.passWord = :password")
			.setString("name", u.getName())
			.setString("licenceID", u.getLicenceID())
			.setString("password", u.getPassWord())
			.uniqueResult();
		
		session.getTransaction().commit();
		
		if(count > 0) return true;
		return false;
		
	}
	
	public boolean isPotentialUser(User user) throws Exception {
		PotentialUser puser = new PotentialUser();
		puser.setLicenceID(user.getLicenceID());
		puser.setName(user.getName());
		Session session = HibernateUtil.getUserSessionFactory().getCurrentSession();
		session.beginTransaction();
		long count = (Long)session.createQuery("select count(*) from PotentialUser potentialUser where potentialUser.name = :name and potentialUser.licenceID = :licenceID")
			.setString("name", user.getName())
			.setString("licenceID", user.getLicenceID())
			.uniqueResult();
		session.getTransaction().commit();
		
		if(count > 0) return true;
		return false;
	}

	public BasicService getBasicS() {
		return basicS;
	}

	public void setBasicS(BasicService basicS) {
		this.basicS = basicS;
	}

	public CustomsService getCustomsS() {
		return customsS;
	}

	public void setCustomsS(CustomsService customsS) {
		this.customsS = customsS;
	}

	public ExportService getExportS() {
		return exportS;
	}

	public void setExportS(ExportService exportS) {
		this.exportS = exportS;
	}

	public FinanceService getFinanceS() {
		return financeS;
	}

	public void setFinanceS(FinanceService financeS) {
		this.financeS = financeS;
	}

	public PermissionService getPermissionS() {
		return permissionS;
	}

	public void setPermissionS(PermissionService permissionS) {
		this.permissionS = permissionS;
	}

	public QualityService getQualityS() {
		return qualityS;
	}

	public void setQualityS(QualityService qualityS) {
		this.qualityS = qualityS;
	}

	public TaxService getTaxS() {
		return taxS;
	}

	public void setTaxS(TaxService taxS) {
		this.taxS = taxS;
	}

	public PresentationService getPresentationS() {
		return presentationS;
	}

	public void setPresentationS(PresentationService presentationS) {
		this.presentationS = presentationS;
	}
	
	

}
